package com.rw.coco.handler.message.reply.handler.command.assistant;

import com.rw.coco.handler.message.reply.handler.command.bean.CocoProp;
import com.rw.coco.handler.message.reply.handler.command.exception.global.NumberOverflowException;
import com.rw.coco.util.CocoStringUtil;
import com.rw.coco.util.config.AnotherNameConfig;
import com.rw.coco.util.config.CocoConfig;
import com.rw.tool.util.string.StringScanner;

/**
 * 属性读取器
 */
public class PropReader {
    public static final String NAME_REGEX = "\\D+";

    public static final String NUMBER_REGEX = "\\d+";

    private final String msg;

    private final StringScanner scanner;

    private final AnotherNameConfig anotherNameConfig;

    public PropReader(String msg, AnotherNameConfig anotherNameConfig) {
        this.msg = msg;
        this.scanner = new StringScanner(msg);
        this.anotherNameConfig = anotherNameConfig;
    }

    public boolean hasNext() {
        return scanner.hasNext(NAME_REGEX + NUMBER_REGEX);
    }

    public CocoProp next() {
        String name = scanner.next(NAME_REGEX);
        try {
            int value = Integer.parseInt(scanner.next(NUMBER_REGEX));
            return new CocoProp(name, value, anotherNameConfig);
        } catch (NumberFormatException exception) {
            throw new NumberOverflowException();
        }
    }

    public boolean done() {
        return !scanner.hasNext();
    }

    public String getErrorPosition() {
        return CocoStringUtil.openWindow(msg, scanner.getIndex(), 4);
    }
}
